<?php
require_once 'modules/network/const.inc';
class Helper_Network extends Zend_Controller_Action_Helper_Abstract
{
    private $_ctrl;
    private $_prefix;
    
    public function init()
    {
        $this->_ctrl = $this->getActionController();    
        $this->_prefix = NETWORK_CACHE_PREFIX;        
    }
    
    public function getUserNetworks($uid, $limit = NETWORK_USER_NETWORK)
    {
        $key = 'user_networks_home_' . $uid;
        $networks = $this->_ctrl->getCache($key, $this->_prefix);
        if ( ! $networks){
            $networkModel = AppGlobal::loadModel('Network', 'network');
            $networks = $networkModel->getUserNetworks($uid, $limit);
            $this->_ctrl->setCache($key, $networks, $this->_prefix);
        }                                                        
        return $networks;
    }    
    
    public function getNetwork($nid)
    {
        $key = 'network_info_' . $nid;
        $network = $this->_ctrl->getCache($key, $this->_prefix);
        if ( ! $network) {                            
            $networkModel = AppGlobal::loadModel('Network', 'network');
            $network = $networkModel->getNetwork($nid);
            $this->_ctrl->setCache($key, $network, $this->_prefix);
        }         
        return $network;     
    }        
    
    public function getNetworkUID($nid)
    {           
        $key = 'network_uids_' . $nid;
        $data = $this->_ctrl->getCache($key, $this->_prefix);
        if ( ! $data){    
            $networkModel = AppGlobal::loadModel('Network', 'network');  
            $data = $networkModel->getNetworkUID($nid);
            $this->_ctrl->setCache($key, $data, $this->_prefix, 21600);            
        }                                                                      
        return $data;
    }   
        
    public function getAllNetworks() 
    {                                   
        $key = 'all_networks';
        $data = $this->_ctrl->getCache($key, $this->_prefix);
        if ( ! $data){
            $networkModel = AppGlobal::loadModel('Network', 'network');
            $data = $networkModel->getAllNetworks();
            $this->_ctrl->setCache($key, $data, $this->_prefix, 21600);
        }
        return $data;
    }      
    
    public function isAdmin($uid)
    {
        return $this->_ctrl->hasPerm(NETWORK_ADMIN);
    } 
    
    public function updateNetworkStats($nid, $data)
    {
        $statsModel = AppGlobal::loadModel('NetworkStats', 'network');
        $statsModel->updateNetworkStatistics($nid, $data);
    }
    
    public function isMember($uid, $nid)
    {
        $networks = $this->getUserNetworks($uid);
        if (count($networks) < 1){
            return FALSE;
        }
        foreach ($networks as $network){
            if ($network->nwid == $nid){
                return TRUE;
            }
        }
        return FALSE;
    }
    
    public function getNetworkByName($name)
    {
        $networkModel = AppGlobal::loadModel('Network', 'network');  
        $network = $networkModel->getNetworkByName($name);    
        if ($network){
            return $network;
        }
        return FALSE;
    }
    
    public function joinNetwork($uid, $nid)
    {
        $networkModel = AppGlobal::loadModel('Network', 'network');  
        $networkModel->joinNetwork($uid, $nid);       
        
        $memberCount = $networkModel->countMembers($nid);
        $statsModel = AppGlobal::loadModel('NetworkStats', 'network');
        $data = array('num_member' => $memberCount);
        $statsModel->updateNetworkStatistics($nid, $data);
                
        $count = $networkModel->countUserNetworks($uid);
        //$this->_ctrl->_helper->Statistic->setUserStatistic($uid, array('count_network' => $count));       
        $this->_ctrl->getHelper('Statistic')->setUserStatistic($uid, array('count_network' => $count));       
    }
}

